<template>
  <div class="container">
    <el-card class="box-card">
      <!-- 头部 -->
     <el-row type="flex" class="row-bg" justify="space-between">
      <el-col :span="18" style=" padding-left: 25px;">
        <el-form  :inline="true">
          <el-form-item label="关键字">
            <el-input style="width:350px" v-model="keyword"></el-input>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="4" style="text-align:right">
         <el-button size="small" @click="delIptValue">清除</el-button>
         <el-button type="primary" size="small" @click="filter">搜索</el-button>
      </el-col>
    </el-row>
      <!-- 中间 -->
      <el-alert
        :title="'数据一共'+total+'条'"
        type="info"
        style="margin: 1px 4px"
        :closable="false"
        show-icon
      >
      </el-alert>

      <!-- 底部表格 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="编号" prop="id" width="200">
        </el-table-column>
        <el-table-column label="题型">
          <template #default={row}>{{row.questionType | handelQuestionType}}</template>
        </el-table-column>
        <el-table-column label="题目编号" width="200">
          <template #default={row}>
            <p v-for="item,index in row.questionIDs" :key="index" style="color:#0099ff;cursor: pointer;" @click="showPreview(item)">{{item.number}}</p>
          </template>

        </el-table-column>
        <el-table-column label="录入时间" prop="addTime" width="150"> </el-table-column>
        <el-table-column label="答题时间(s)" prop="totalSeconds"> </el-table-column>
        <el-table-column label="正确率(%)" prop="accuracyRate"> </el-table-column>
        <el-table-column label="录入人" prop="userName"> </el-table-column>
        <el-table-column label="操作">
          <template #default={row}>
            <el-button type="danger" icon="el-icon-delete" circle @click="delRandom(row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页符 -->
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[20, 30, 50, 100]"
            :page-size="pagesize"
            layout="prev, pager, next, sizes, jumper"
            :total="total">
          </el-pagination>
      </div>

      <!-- 预览弹框 -->
      <QuestionPreview ref="questionPreview" :dialogVisible.sync="dialogVisible" :data="questionInfo"></QuestionPreview>

    </el-card>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
import { questionType } from '@/api/hmmm/constants'
import QuestionPreview from '@/module-hmmm/components/questions-preview'
export default {
  components: { QuestionPreview },
  data () {
    return {
      tableData: [], // 表格列表数据
      total: 0, // 数据记录条数
      page: 1,
      pagesize: 20,
      keyword: '',
      dialogVisible: false, // 预览弹框
      questionInfo: {} // 预览框信息
    }
  },
  created () {
    this.getRandomsList()
  },
  filters: {
    handelQuestionType (value) {
      const res = questionType.find(item => item.value === +value)
      // console.log();
      return res ? res.label : '未知'
    }

  },
  methods: {
    // 渲染组题列表
    async getRandomsList () {
      const { data } = await randoms({ page: this.page, pagesize: this.pagesize, keyword: this.keyword })
      // console.log(data)
      this.tableData = data.items
      this.total = data.counts
    },

    // 分页符操作
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      // console.log(this.pagesize)
      this.getRandomsList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      // console.log(this.page)
      this.getRandomsList()
    },

    // 点击清除，删除输入框内容
    delIptValue () {
      this.keyword = ''
      this.getRandomsList()
    },

    // 搜索筛选关键字
    filter () {
      this.page = 1
      this.getRandomsList()
    },

    // 删除功能
    delRandom (id) {
      this.$confirm('此操作将永久删除该消息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.$message.success('删除成功!')
        await removeRandoms({ id })
        this.getRandomsList()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },

    // 预览效果
    showPreview (questionInfo) {
      this.dialogVisible = true
      this.questionInfo = questionInfo
      this.$nextTick(() => {
        this.$refs.questionPreview.open()
      })
    }

  }
}
</script>
<style scoped lang="scss">
.text {
  font-size: 14px;
}

.box-card {
  margin: 10px 10px 0 10px;
  // .el-form-item__label{

  // }
}

.block{
  float: right;
  height: 50px;
  margin-top: 10px;
}
</style>
